package edu.moravian.utilities;

/**
 * This class is intended to be used as a Game Timer
 * 
 * It calculates the "frame" length of calls between 
 * tick and has accessor methods for instantaneous FPS and
 * the delta of the previous frame
 * 
 * @author Elliot
 */
public class Timer 
{
    private static final int MILLIS_PER_SEC = 1000;
    
    //the start time of the current frame in milliseconds
    private long frameStartTimeInMillis;
    //the length of the previous frame in milliseconds
    private long frameLengthInMillis;
    
    
    /**
     * Create a new timer. The FPS and Delta will be zero until tick is called
     */
    public Timer()
    {
        frameStartTimeInMillis = System.currentTimeMillis();
        frameLengthInMillis = 0;
    }

    
    /**
     * Update the Timer. The current frame length will be the current time 
     * minus the last time tick was called or the timer was created
     */
    public void tick()
    {
        long frameEndTime = System.currentTimeMillis();
        
        frameLengthInMillis = frameEndTime - frameStartTimeInMillis;
        
        frameStartTimeInMillis = frameEndTime;
    }
    
   
    /**
     * Get the fps of the previous frame in seconds 
     * 
     * @return the FPS of the previous frame in seconds
     */
    public double getFPS()
    {
        double fps = (double)MILLIS_PER_SEC/frameLengthInMillis;
        
        return fps; 
    }
   
    
    /**
     * Get the length of the previous frame in seconds
     * 
     * @return the previous frame length in seconds 
     */
    public double getDelta()
    {
        return (double)frameLengthInMillis/MILLIS_PER_SEC; 
    }
    
}
